//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "Greeks.h"
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::CGreeks::CGreeks (boost::shared_ptr<QuantLib::Greeks>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phGreeks = NULL;
#endif
	_ppGreeks = &childNative;
    
}
Cephei::QL::CGreeks::CGreeks (QuantLib::Greeks& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phGreeks = NULL;
#endif
	_ppGreeks = new boost::shared_ptr<QuantLib::Greeks> (&childNative);
    
    _GreeksOwner = owner;
    
}

Cephei::QL::CGreeks::CGreeks (CGreeks^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phGreeks = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppGreeks = new boost::shared_ptr<QuantLib::Greeks> (copy->GetShared());
        
    }
}
Cephei::QL::CGreeks::CGreeks (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phGreeks = NULL;
#endif
	if (!t->IsSubclassOf(CGreeks::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::CGreeks::CGreeks (QuantLib::Handle<QuantLib::Greeks>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phGreeks = &childNative;
	_ppGreeks = &static_cast<boost::shared_ptr<QuantLib::Greeks>>(childNative.currentLink());
    
    _GreeksOwner = owner;
}
Cephei::QL::CGreeks::CGreeks (QuantLib::Handle<QuantLib::Greeks> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phGreeks = &childNative;
	_ppGreeks = &static_cast<boost::shared_ptr<QuantLib::Greeks>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::CGreeks::CGreeks (QuantLib::Greeks childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phGreeks = NULL;
#endif
	_ppGreeks = new boost::shared_ptr<QuantLib::Greeks> (new QuantLib::Greeks (childNative));
    
}
#endif

Cephei::QL::CGreeks::~CGreeks ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppGreeks != NULL)
    {
	    delete _ppGreeks;
        _ppGreeks = NULL;
    }
}
Cephei::QL::CGreeks::!CGreeks ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppGreeks != NULL)
    {
	    delete _ppGreeks;
    }
}
QuantLib::Greeks& Cephei::QL::CGreeks::GetReference ()
{
    if (_ppGreeks == NULL) throw gcnew NativeNullException ();
	return **_ppGreeks;
}
boost::shared_ptr<QuantLib::Greeks>& Cephei::QL::CGreeks::GetShared ()
{
    if (_ppGreeks == NULL) throw gcnew NativeNullException ();
	return *_ppGreeks;
}
QuantLib::Greeks* Cephei::QL::CGreeks::GetPointer ()
{
    if (_ppGreeks == NULL) throw gcnew NativeNullException ();
	return &**_ppGreeks;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::Greeks>& Cephei::QL::CGreeks::GetHandle ()
{
	if (_phGreeks == NULL)
	{
		_phGreeks = new Handle<QuantLib::Greeks> (*_ppGreeks);
	}
	return *_phGreeks;
}
#endif
bool Cephei::QL::CGreeks::HasNative () 
{
	return (_ppGreeks != NULL);
}

Cephei::QL::IGreeks^ Cephei::QL::CGreeks::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppGreeks)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

